<!DOCTYPE html>
<html lang="zh-CN">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="renderer" content="webkit">
	<meta http-equiv="Content-Type" content="text/html">
	<title>账号密码登录</title>
	<style type="text/css">
		.STYLE1 {
			font-size: 12px
		}

		/* 验证码加载动画 */
		@keyframes captchaLoading {
			0% { opacity: 0.5; }
			50% { opacity: 0.8; }
			100% { opacity: 0.5; }
		}

		.captcha-loading {
			animation: captchaLoading 1s infinite;
			transition: opacity 0.3s ease;
		}

		.captcha-ready {
			opacity: 1;
			transition: opacity 0.3s ease;
		}

		button {
			margin: 0px;
			width: 100px;
			font-size: 0px;
			height: 43px;
			border-radius: 0px;
			border: 0px solid red;
			outline: none;
			background: rgba(0, 0, 0, 0);
		}

		.kuang {
			position: relative;
			width: 250px;
			height: 43px;
		}

		.lookpsw {
			position: absolute;
			top: 1px;
			right: 13px;
			width: 30px;
			height: 30px;
			background-size: 100% 100%;
			cursor: pointer;
		}

		.lookpsw.hide {
			background-size: 100% 100%;
		}

		.STYLE6 {
			color: #999999;
			font-size: 12px;
		}

		a.gray {
			color: #999999;
			text-decoration: none
		}

		a.gray:hover {
			color: #CCCCCC;
			text-decoration: none
		}
	</style>
</head>

<body>

	<table width="100%" height="100%" border="0" cellpadding="1" cellspacing="0" class="TitleColor">
		<tr>
			<td width="30%" height="100%"></td>
			<td width="40%" height="100%">
				<table width="100%" border="0" cellpadding="1" cellspacing="0">
					<tr>
						<td>
							<table align="center" border="0" cellspacing="0" cellpadding="1" width="420px">
								<tr>
									<td align="center" style="width:140px;">
										<div align="center">账号密码登录</div>
									</td>
									<td align="center" style="width:140px;">
										<div align="center"><a href="mobile.htm">手机验证码登录</a></div>
									</td>
									<td align="center" style="width:140px;">
										<div align="center"><a href="email.htm">邮箱验证码登录</a></div>
									</td>
								</tr>
								<tr>
									<td>&nbsp;</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table align="center" border="0" cellspacing="0" cellpadding="1">
								<tr>
									<td colspan="3"><input type="text" id="name" placeholder="用户名"
											style="height:43px;width:250px;"></td>
								</tr>
								<tr>
									<td>&nbsp;</td>
								</tr>
								<tr>
									<td colspan="3">
										<div class="kuang">
											<input type="password" id='psw' placeholder="密码"
												style="height:43px;width:250px;"
												oninput="value=value.replace(/[\u4E00-\u9FA5]/g,'')">
											<div class="lookpsw" id='clickImg'><img src="" id="showImg" alt="">
											</div>
										</div>
									</td>
								</tr>
								<tr>
									<td>&nbsp;</td>
								</tr>
								<tr>
									<td align="center"><input type="text" value="" placeholder="验证码（区分大小写）"
											style="height:43px;width:140px" id="code" maxlength="4"
											oninput="value=value.replace(/[^a-zA-Z0-9]/g,'')"></td>
									<td> <button type="button" onClick="CodeonClick()" title="验证码"><img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='43' viewBox='0 0 100 43'%3E%3Crect width='100' height='43' fill='%23f0f0f0'/%3E%3C/svg%3E" value=""
												width="100" height="43" id="ivc" alt="验证码" style="cursor:pointer;"></button></td>
								</tr>
								<tr>
									<td>&nbsp;</td>
								</tr>
								<tr>
									<td colspan="3" align="center"><input type="submit" onClick="login()" value="登录"
											style="height:43px;width:250px;"></td>
								</tr>
								<tr>
									<td>&nbsp;</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table align="center" border="0" cellspacing="0" cellpadding="1">
								<tr>
									<td align="center" style="width:125px;">
										<div align="left" class="STYLE1"><a href="forgot.htm">忘记密码？</a></div>
									</td>
									<td align="center" style="width:125px;">
										<div align="right"><a href="register.htm" class="STYLE1">立即注册</a></div>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr height="150">
						<td class="STYLE6" align="center">
							<div id='footerpage'></div>
						</td>
					</tr>
				</table>
			</td>
			<td width="30%" height="100%"></td>
		</tr>
	</table>
</body>
<!--
<script src="js/blueimp-md5-2.18.0.js"></script>
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/base64.js"></script>
-->
<script>
	// 优化的脚本加载函数
	function loadScript(src, callback) { 
		var script = document.createElement("script"); 
		script.type = "text/javascript"; 
		var content = sessionStorage.getItem(src); 
		
		if (content) {
			// 从缓存加载脚本
			script.textContent = content;
			document.head.appendChild(script);
			if (callback) callback(script);
		} else {
			// 首次加载时从服务器获取
			fetch(src)
				.then(function(response) { return response.text(); })
				.then(function(content) { 
					script.textContent = content; 
					sessionStorage.setItem(src, content); 
					document.head.appendChild(script);
					if (callback) callback(script);
				})
				.catch(function(error) { 
					console.error("加载脚本失败:", error);
					alert("加载脚本失败，请刷新页面重试");
				});
		}
	}

	// 加载主脚本
	loadScript("tu_0.js", function(script) {
		// 脚本加载完成后，初始化页面
		if (typeof initPage === 'function') {
			initPage();
		} else {
			// 如果initPage函数还不可用，等待DOM加载完成后再尝试
			if (document.readyState === 'loading') {
				document.addEventListener('DOMContentLoaded', function() {
					setTimeout(initPage, 100);
				});
			} else {
				setTimeout(initPage, 100);
			}
		}
	});
</script>
<script>
	// 简化的密码处理函数
	function encryptPassword(password) {
		return btoa(password.split('').reverse().join(''));
	}

	function decryptPassword(encryptedPassword) {
		try {
			// 检查是否为有效的Base64字符串
			if (encryptedPassword && typeof encryptedPassword === 'string') {
				return atob(encryptedPassword).split('').reverse().join('');
			}
			return '';
		} catch (e) {
			console.error("解码密码时出错:", e);
			return '';
		}
	}

	function storePassword(password) {
		try {
			sessionStorage.setItem('regpsw', encryptPassword(password));
			var username = document.getElementById("name").value;
			if (username) {
				sessionStorage.setItem('regkcid', encodeURIComponent(username));
			}
		} catch (e) {
			console.error("存储密码时出错:", e);
		}
	}

	function retrievePassword() {
		try {
			var encryptedPsw = sessionStorage.getItem('regpsw');
			return encryptedPsw ? decryptPassword(encryptedPsw) : '';
		} catch (e) {
			console.error("获取密码时出错:", e);
			return '';
		}
	}

	// 全局变量
	var counter = 0;
	var verifycode = 0;
	var pageInitialized = false;
	var captchaLoaded = false; // 添加验证码加载状态标记
	
	// 优化的页面初始化函数
	function initPage() {
		// 防止重复初始化
		if (pageInitialized) return;
		
		try {
			// 初始化页面元素
			if (typeof loadHtml === 'function') {
				// 加载HTML和图片资源
				loadHtml("footer.htm", "footerpage");
				loadPhoto("img/pswlook.png", "showImg");
				
				var password = document.getElementById('psw');
				var onclick = document.getElementById('clickImg');
				if (password && onclick && typeof lookImg === 'function') {
					lookImg(password, onclick, "showImg");
				}
				
				// 确保页面编码正确
				document.characterSet = "UTF-8";
				
				// 填充保存的用户名和密码
				var savedUsername = sessionStorage.getItem('regkcid');
				if (savedUsername) {
					// 如果是编码过的，尝试解码
					if (savedUsername.indexOf('%') !== -1) {
						try {
							savedUsername = decodeURIComponent(savedUsername);
						} catch (e) {
							// 解码失败时使用原始值
						}
					}
					document.getElementById('name').value = savedUsername;
				}
				
				// 填充密码时添加额外检查
				var savedPassword = retrievePassword();
				if (savedPassword && document.getElementById('psw')) {
					document.getElementById('psw').value = savedPassword;
				}
				
				// 添加事件监听器
				document.addEventListener('keypress', function(e) {
					if (e.key === 'Enter') {
						e.preventDefault();
						login();
					}
				});
				
				// 标记页面已初始化
				pageInitialized = true;
				console.log("页面初始化完成");
				
				// 页面初始化完成后自动加载验证码
				setTimeout(function() {
					autoLoadCaptcha();
				}, 300); // 短暂延迟确保其他资源加载完成
			} else {
				// 如果必要的函数还未加载，稍后再试
				console.log("等待脚本加载完成...");
				setTimeout(initPage, 200);
			}
		} catch (e) {
			console.error("初始化页面时出错:", e);
			// 即使出错也继续加载验证码
			setTimeout(function() {
				autoLoadCaptcha();
			}, 300);
		}
	}

	// 验证码刷新按钮点击处理
	function CodeonClick() {
		// 强制刷新验证码，忽略captchaLoaded状态
		captchaLoaded = false; // 重置状态以确保可以刷新
		console.log("手动刷新验证码");
		refreshCaptchaImmediately();
	}

	function login() {
		var name = document.getElementById("name").value;
		var password = document.getElementById("psw").value;
		var jpgcode = document.getElementById("code").value;
		
		if (!name || !password || !jpgcode) {
			alert("请填写完整信息");
			return false;
		}
		
		// if (password.length < 6) {
		// 	alert('密码长度不能少于6个字符');
		// 	return false;
		// }
		
		storePassword(password);
		myTimer("userlogin");
	}

	// 自动加载验证码的函数
	function autoLoadCaptcha() {
		if (!pageInitialized || captchaLoaded) {
			console.log("跳过验证码自动加载：页面未初始化或验证码已加载");
			return;
		}
		refreshCaptchaImmediately();
	}

	// 优化的验证码系统
	function refreshCaptchaImmediately() {
		var imgElement = document.getElementById("ivc");
		var codeInput = document.getElementById("code");
		
		// 更新样式和状态 - 显示加载中状态，不显示文字
		imgElement.src = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='43' viewBox='0 0 100 43'%3E%3Crect width='100' height='43' fill='%23f0f0f0'/%3E%3C/svg%3E";
		imgElement.className = "captcha-loading";
		imgElement.style.cursor = "wait";
		imgElement.alt = "验证码加载中";
		imgElement.title = "验证码加载中";
		codeInput.value = "";
		
		console.log("开始加载验证码");
		
		var xhr = postPhp("index.php");
		xhr.onreadystatechange = function() {
			if (xhr.readyState === 4) {
				if (xhr.status === 200) {
					var array_name = txtUnzip(xhr);
					if (array_name != null && array_name.ivc) {
						verifycode = array_name.ivc;
						imgElement.src = "ivc" + array_name.ivc + ".png";
						imgElement.className = "captcha-ready";
						imgElement.style.cursor = "pointer";
						imgElement.alt = "验证码";
						imgElement.title = "点击刷新验证码";
						captchaLoaded = true; // 标记验证码已加载
						codeInput.focus();
						console.log("验证码加载成功");
					} else {
						handleCaptchaError("验证码数据无效");
					}
				} else {
					handleCaptchaError("网络请求失败");
				}
			}
		};
		
		xhr.onerror = function() {
			handleCaptchaError("网络连接错误");
		};
		
		var cnt = ++counter;
		txtZip(xhr, "t:nivc&cnta:" + cnt);
	}
	
	// 处理验证码加载错误
	function handleCaptchaError(message) {
		var imgElement = document.getElementById("ivc");
		console.error("验证码加载失败:", message);
		
		// 显示错误信息，提示用户重试
		imgElement.src = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='43' viewBox='0 0 100 43'%3E%3Crect width='100' height='43' fill='%23fff0f0'/%3E%3Ctext x='50' y='21.5' font-family='Arial' font-size='12' text-anchor='middle' dominant-baseline='middle' fill='%23cc0000'%3E点击重新加载%3C/text%3E%3C/svg%3E";
		imgElement.className = "";
		imgElement.style.cursor = "pointer";
		imgElement.alt = "点击重新加载验证码";
		imgElement.title = "点击重新加载验证码";
		captchaLoaded = false; // 重置加载状态
	}

	// 更简化的myTimer函数
	function myTimer(type) {
		// 登录请求处理
		var xhr = postPhp("index.php");
		
		xhr.onreadystatechange = function() {
			if (xhr.readyState === 4 && xhr.status === 200) {
				var array_name = txtUnzip(xhr);
				if (array_name != null && type == "userlogin") {
					loginFunction(array_name);
				} else if (array_name == null) {
					alert("登录失败，请重试");
				}
			} else if (xhr.readyState === 4) {
				alert("网络请求失败，请重试");
			}
		};
		
		if (type == "userlogin") {
			var nameValue = document.getElementById("name").value;
			var codeValue = document.getElementById("code").value;
			var pswValue = document.getElementById("psw").value;
			
			var encodedName = encodeURIComponent(nameValue);
			var name = txtB64Enc(encodedName);
			var code = txtB64Enc(codeValue);
			var vmd5 = md5(pswValue);
			var psw = vmd5.substring(0, 6);
			
			var data = "t:sbul&name:" + name + 
					  "&pswa:" + psw + 
					  "&code:" + code + 
					  "&ivca:" + verifycode + 
					  "&host:" + encodeURIComponent(navigator.userAgent);
			
			txtZip(xhr, data);
		}
	}

	function loginFunction(arr) {
		if (!arr || !arr.result || arr.result == "repeat") {
			alert("登录失败，请重试");
			refreshCaptchaImmediately();
			return false;
		}
		switch (arr.result) {
			case "ok":
				var username = document.getElementById("name").value;
				if (username) {
					sessionStorage.setItem('regkcid', encodeURIComponent(username));
				}
				window.location.href = "center.htm";
				break;
			default:
				alert(arr.result === "code" ? "验证码错误" : "账号或密码错误:" + arr.result);
				refreshCaptchaImmediately();
				return false;
		}
	}
</script>

</html>